//
//  FZ_BoFang_Two_TableViewCell_Two.m
//  ProjectOfBStage
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "FZ_BoFang_Two_TableViewCell_Two.h"
#import "FZ_BoFang_Two_TableViewCell_Two_First_Cell.h"
#import "FZ_BoFang_Two_TableViewCell_Two_Second_Cell.h"
#import "FZ_BoFang_Two_TableViewCell_Two_Three_Cell.h"
#import "FZ_BoFang_Two_TableViewCell_Two_Fourth_Cell.h"
#import "FZ_BoFang_Two_TableViewCell_Two_Fifth_Cell.h"
#import "BaseHeader.h"
#import "FZ_Dounment.h"
#import "CoreDataManager.h"
#import "FZ_BoFang_Two_TableViewCell_Two_CollectionCell.h"
@interface FZ_BoFang_Two_TableViewCell_Two ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,retain) UITableView *tableViewOne;
@property (nonatomic ,strong) CoreDataManager *coreDataManagerX;
@property (nonatomic ,retain) NSArray *coreResultArrayX;
@end
@implementation FZ_BoFang_Two_TableViewCell_Two

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createSubView];
        
    }
    return self;
}

- (void)createSubView{
    self.coreDataManagerX = [CoreDataManager shareManager];
    NSLog(@"url:%@",[self.coreDataManagerX applicationDocumentsDirectory]);
    self.tableViewOne = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 200.f) style:UITableViewStylePlain];
    self.tableViewOne.delegate = self;
    self.tableViewOne.bounces = NO;
    self.tableViewOne.dataSource = self;
    self.tableViewOne.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.tableViewOne];
    [_tableViewOne release];

    
    [self.tableViewOne registerClass:[FZ_BoFang_Two_TableViewCell_Two_First_Cell class] forCellReuseIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_First_Cell"];
    [self.tableViewOne registerClass:[FZ_BoFang_Two_TableViewCell_Two_Second_Cell class] forCellReuseIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_Second_Cell"];
    [self.tableViewOne registerClass:[FZ_BoFang_Two_TableViewCell_Two_Three_Cell class] forCellReuseIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_Three_Cell"];
    [self.tableViewOne registerClass:[FZ_BoFang_Two_TableViewCell_Two_Fourth_Cell class] forCellReuseIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_Fourth_Cell"];
    [self.tableViewOne registerClass:[FZ_BoFang_Two_TableViewCell_Two_Fifth_Cell class] forCellReuseIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_Fifth_Cell"];
    
    //自定义分割线
//    UIView * separator = [[UIView alloc] initWithFrame:CGRectMake(10,  10 , 380, 5)];
//    separator.backgroundColor = [UIColor redColor];
//    [_tableViewOne addSubview:separator];
    
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.tableViewOne.frame = self.contentView.frame;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.row == 0) {
        FZ_BoFang_Two_TableViewCell_Two_First_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_First_Cell"];
        cell.modelF = self.JieShouModelS;
            cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row == 1) {
        FZ_BoFang_Two_TableViewCell_Two_Second_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_Second_Cell"];
        cell.modelS = self.JieShouModelS;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row == 2) {
        FZ_BoFang_Two_TableViewCell_Two_Three_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_Three_Cell"];
        cell.modelT = self.JieShouModelS;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row == 3) {
        FZ_BoFang_Two_TableViewCell_Two_Fourth_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_Fourth_Cell"];
        cell.modelFo = self.JieShouModelS;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else{
        FZ_BoFang_Two_TableViewCell_Two_Fifth_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"FZ_BoFang_Two_TableViewCell_Two_Fifth_Cell"];
        cell.modelFi = self.JieShouModelS;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
            
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {

        [self.delegate BoFangXingQing:nil];

    }
    else if(indexPath.row == 4) {
        [self.fzFenXiangDelegate FZFenXiangXingQing:nil andModel:nil andString:nil];
    }
    else{
        [self SeachCore];
        [self.fzDelegate FZBoFangXingQing:self.coreResultArrayX andModel:nil];
    }
    
}

- (void)SeachCore{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"FZ_Dounment" inManagedObjectContext:self.coreDataManagerX.managedObjectContext];
    [fetchRequest setEntity:entity];

//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS '最甜单身情歌「我喜欢一个人」'"];
//
//    [fetchRequest setPredicate:predicate];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.coreDataManagerX.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    self.coreResultArrayX = fetchedObjects;
    if (fetchedObjects == nil) {
        NSLog(@"error:%@",error);
    }
    else{
        for (FZ_Dounment *coreModelF  in fetchedObjects) {
            NSLog(@"name :%@,pic:%@,source:%@",coreModelF.name,coreModelF.pic,coreModelF.source);
        }
    }
}

- (void)awakeFromNib {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
